package com.fmdj.vhr.service.impl;

import com.fmdj.common.exception.GlobalException;
import com.fmdj.vhr.db.dao.VoucherCustomerDao;
import com.fmdj.vhr.db.dao.VoucherDao;
import com.fmdj.vhr.service.VoucherCustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class VoucherCustomerServiceImpl implements VoucherCustomerService {

    @Autowired
    private VoucherCustomerDao voucherCustomerDao;

    @Autowired
    private VoucherDao voucherDao;

    @Override
    public String userVoucher(Map map) {
        //检查优惠券是否可用
        String discount = voucherCustomerDao.validCanUseVoucher(map);
        if (discount !=null) {
            //优惠券可用,绑定代金卷
            int rows = voucherCustomerDao.bindVoucher(map);
            if (rows!=1) {
                throw new GlobalException("绑定代金卷失败");
            }
            return discount;
        }
        throw new GlobalException("代金券不可用");
    }
}
